
import {useEffect,useState} from "react";
import useGetUserInfo from "./useGetUserInfo";
import {useRequest} from "ahooks";
import {getUserInfoServe} from "../services/user";
import {useDispatch} from "react-redux";
import {loginRuder} from "../store/userReducer";



function useLoadingUserData(){

    const dispath =  useDispatch();
    const [waiting,setWaiting] = useState(true);
    const {run:getUserInfo} =useRequest(  getUserInfoServe,{
        manual:true,
        onSuccess(res){
         const {username,naLable}  =  res;
         //存储到redux中
           dispath(loginRuder({username,naLable}));
        },
        onFinally(){
            setWaiting(false);
        }
    })

    //获取用户信息有没有
    const {username} = useGetUserInfo();
    useEffect(()=>{
        //如果存在用户信息就不用发送ajax请求
        if(username){
            setWaiting(false);
            return;
        }
        getUserInfo();//没有用户信息就加载ajax请求
    },[username])



    //进行ajax的请求，然后相关的结果返回给redux中




    return {waiting}
}

export default useLoadingUserData;